/**
 * Copyright 2013 yangming.liu<liuyangming@gmail.com>.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, see <http://www.gnu.org/licenses/>.
 */
package org.bytesoft.openjta.internal;

import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

import org.bytesoft.utils.CommonUtils;

public class XAResourceWrapper implements XAResource {
	protected XAResource delegate;
	protected boolean xaSupport;

	public XAResourceWrapper() {
	}

	public XAResourceWrapper(XAResource xares, boolean xaSupport) {
		this.delegate = xares;
		this.xaSupport = xaSupport;
	}

	public void commit(Xid xid, boolean onePhase) throws XAException {
		delegate.commit(xid, onePhase);
	}

	public void end(Xid xid, int flags) throws XAException {
		delegate.end(xid, flags);
	}

	public void forget(Xid xid) throws XAException {
		delegate.forget(xid);
	}

	public int getTransactionTimeout() throws XAException {
		return delegate.getTransactionTimeout();
	}

	public boolean isSameRM(XAResource xares) throws XAException {
		return delegate.isSameRM(xares);
	}

	public int prepare(Xid xid) throws XAException {
		return delegate.prepare(xid);
	}

	public Xid[] recover(int flag) throws XAException {
		return delegate.recover(flag);
	}

	public void rollback(Xid xid) throws XAException {
		delegate.rollback(xid);
	}

	public boolean setTransactionTimeout(int seconds) throws XAException {
		return delegate.setTransactionTimeout(seconds);
	}

	public void start(Xid xid, int flags) throws XAException {
		delegate.start(xid, flags);
	}

	public XAResource getDelegate() {
		return delegate;
	}

	public void setDelegate(XAResource delegate) {
		this.delegate = delegate;
	}

	public boolean isXaSupport() {
		return xaSupport;
	}

	public void setXaSupport(boolean xaSupport) {
		this.xaSupport = xaSupport;
	}

	@Override
	public int hashCode() {
		int hash = 17;
		hash += 19 * ((this.delegate == null) ? 0 : this.delegate.hashCode());
		return hash;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null) {
			return false;
		} else if (this.getClass().equals(obj.getClass())) {
			XAResourceWrapper that = (XAResourceWrapper) obj;
			return CommonUtils.equals(this.delegate, that.delegate);
		}
		return false;
	}

	public String toString() {
		return String.format("XAResourceWrapper [xa-support: %5s, delegate= %s] ", this.xaSupport, this.delegate);
	}
}
